package vn.buffalocoders.rikos.ui.common;

import java.util.Date;

import vn.buffalocoders.rikos.R;
import android.content.Context;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

public class Helper {
	private static int USERNAME_MIN_LEN = 6;
	private static int PASSWORD_MIN_LEN = 6;

	public static boolean validateForSignUpActivity(Context context, TextView mValidateText, EditText mUserView, EditText mEmailView, EditText mPasswordView) {
		boolean bCheckUsername = true;

		String user = mUserView.getText().toString();
		
		String errUser = "";
		
		// Check for valid username
		if (TextUtils.isEmpty(user)) {
			bCheckUsername = false;
			errUser = context.getResources().getString(R.string.error_username_empty).toString();
		}
		else if (user.length() < USERNAME_MIN_LEN)
		{
			bCheckUsername = false;
			errUser = String.format(context.getResources().getString(R.string.error_username_minimum_len), USERNAME_MIN_LEN);
		}
		else if (user.trim().length() == 0)
		{
			bCheckUsername = false;
			errUser = context.getResources().getString(R.string.error_username_all_whitespace);			
		}
		if (bCheckUsername) {
			mUserView.setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.icon_check), null);
		}
		else {
			mUserView.setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.icon_error), null);
			mUserView.setFocusable(true);
			mUserView.requestFocus();
		}
		
		mValidateText.setVisibility(bCheckUsername ? TextView.INVISIBLE : TextView.VISIBLE);
		if (!bCheckUsername) {
			mValidateText.setText(errUser);
		}
		
		if (!bCheckUsername)
			return bCheckUsername;
		return validateForLogInActivity(context, mValidateText, mEmailView, mPasswordView);
	}
	
	public static boolean validateForLogInActivity(Context context, TextView mValidateText, EditText mEmailView, EditText mPasswordView) {
		String email 	= mEmailView.getText().toString();
		String password = mPasswordView.getText().toString();
		
		boolean bCheckEmail = true;
		boolean bCheckPassword = true;
		
		String errEmail = "", errPass = "";
		
		// Check for valid email
		if (TextUtils.isEmpty(email)) {
			bCheckEmail = false;
			errEmail = context.getResources().getString(R.string.error_email_empty);
		}
		else if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches())
		{
			bCheckEmail = false;
			errEmail = context.getResources().getString(R.string.error_email_invalid_format);
		}
		// Check for valid password
		if (TextUtils.isEmpty(password)) {
			bCheckPassword = false;
			errPass = context.getResources().getString(R.string.error_password_empty);
		}
		else if (password.length() < PASSWORD_MIN_LEN)
		{
			bCheckPassword = false;
			errPass = String.format(context.getResources().getString(R.string.error_password_minimum_len), PASSWORD_MIN_LEN);
		}
		
		if (bCheckEmail) {
			mEmailView.setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.icon_check), null);
		}
		else {
			mEmailView.setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.icon_error), null);
		}
		
		if (bCheckPassword) {
			mPasswordView.setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.icon_check), null);
		}
		else {
			mPasswordView.setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.icon_error), null);
		}
		
		mValidateText.setVisibility(bCheckEmail && bCheckPassword ? TextView.INVISIBLE : TextView.VISIBLE);
		if (!bCheckEmail) {
			mValidateText.setText(errEmail);
			mEmailView.setFocusable(true);
			mEmailView.requestFocus();
		}
		else if (!bCheckPassword) {
			mValidateText.setText(errPass);
			mPasswordView.setFocusable(true);
			mPasswordView.requestFocus();
		}
		
		return bCheckEmail && bCheckPassword;
	}
	
	public static Date ConvertStrToDate(String time)
	{
		long unixSeconds = Long.valueOf(time);
		return new Date(unixSeconds*1000L);
	}
}
